<?php
/**
 * FastApp.
 * 9/22/24
 * @Author deepsea159
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp\Api\Service\Account;

use App\Fastapp\Api\Service\BaseService;
use App\Fastapp\Model\AccountRecharge;
use App\Fastapp\Model\AccountWalletAddressLog;
use App\Fastapp\Model\AccountWalletLog;
use App\Fastapp\Model\AccountWithdrawal;
use Carbon\Carbon;

class WalletLogService extends BaseService
{
    public function list(array $param): array
    {
        $map['user_id'] = get_uid();
        $query = AccountWalletLog::query();
        $with = ['currency:symbol,icon'];
        if (!empty($param['type'])) $map['type'] = $param['type'];
        if (!empty($param['object_type'])) {
            if ($param['object_type'] == 2)$with[] = 'withdrawal:id,charge';
            $query->whereIn('object_type', explode(',', $param['object_type']));
        }
        if (!empty($param['start_at'])) {
            $btw = [$param['start_at'] . ' 00:00:00', Carbon::createFromFormat('Y-m-d', $param['end_at'])->addDay()->toDateString() . ' 00:00:00'];
            $query->whereBetween('created_at', $btw);
        }
        $field = ['id', 'amount', 'created_at', 'symbol', 'type', 'object_type', 'object_id', 'before', 'after'];
        $paginate = $query->with($with)->select($field)->where($map)->orderByDesc('id')->simplePaginate();
        return ['list' => $paginate->items()];
    }

    public function withdrawal(array $param): array
    {
        $query = AccountWithdrawal::query()->where(['user_id' => get_uid()]);
        if (!empty($param['start_at'])) {
            $btw = [$param['start_at'] . ' 00:00:00', Carbon::createFromFormat('Y-m-d', $param['end_at'])->addDay()->toDateString() . ' 00:00:00'];
            $query->whereBetween('created_at', $btw);
        }
        $paginate = $query->where(['user_id' => get_uid()])->simplePaginate();
        return ['list' => $paginate->items()];
    }

    public function recharge(array $param): array
    {
        $query = AccountRecharge::query()->where(['user_id' => get_uid()]);
        if (!empty($param['start_at'])) {
            $btw = [$param['start_at'] . ' 00:00:00', Carbon::createFromFormat('Y-m-d', $param['end_at'])->addDay()->toDateString() . ' 00:00:00'];
            $query->whereBetween('created_at', $btw);
        }
        $paginate = $query->where(['user_id' => get_uid()])->simplePaginate();
        return ['list' => $paginate->items()];
    }

    public function detail(array $param): array
    {
        $currency = get_currency_cache($param['symbol']);
        if ($currency['type'] == 1) {
            if ($param['object_type'] == 1) $data = AccountRecharge::query()->findOrFail($param['object_id']);
            elseif ($param['object_type'] == 2) $data = AccountWithdrawal::query()->findOrFail($param['object_id']);
            else  $data = AccountWalletLog::query()->findOrFail($param['id']);
            $data->symbol = $currency['symbol'];
            return  $data->toArray();
        }
        $data = AccountWalletAddressLog::query()->findOrFail($param['object_id']);
        if ($data->type == 2) {
            $data->charge = AccountWithdrawal::query()->where(['currency_id' => $currency['id'], 'pay_info_id' => $data->id])->value('charge');
        }
        return $data->toArray();
    }
}